package com.wjpeng.wms.service.impl;

import com.wjpeng.wms.domain.Permission;
import com.wjpeng.wms.mapper.PermissionMapper;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IPermissionService;
import com.wjpeng.wms.util.MD5;
import com.wjpeng.wms.util.PageResult;
import com.wjpeng.wms.util.PermissionUtil;
import com.wjpeng.wms.util.RequiredPermission;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void saveOrUpdate(Permission d) {
        if(d.getId() == null){
            permissionMapper.insert(d);
        }else{
            permissionMapper.updateByPrimaryKey(d);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            permissionMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageResult result(QueryObject qo) {
        Integer count = permissionMapper.queryCount(qo);
        if(count == 0){
            return new PageResult(Collections.EMPTY_LIST,0,1,0);
        }
        return new PageResult(permissionMapper.queryLimit(qo),count,qo.getCurrentPage(),qo.getPageSize());
    }

    @Autowired
    private ApplicationContext ctx;//注入容器对象

    @Override
    public void reload() {
        //1:先查询出数据库所有的权限
        List<String> exps = permissionMapper.selectAllExpression();
        //2:获取所有贴有Controller注解的控制器对象
        Collection<Object> beans = ctx.getBeansWithAnnotation(Controller.class).values();
        for (Object bean : beans) {
            Method[] methods = bean.getClass().getDeclaredMethods();//获取控制器的字节码对象,然后获取所有的方法
            for (Method method : methods) {//判断每一个方法是否需要生成权限
                //给权限表达式expression拼接名称
                String expression = PermissionUtil.buildEmpression(method);
                //获取该方法的贴有RequiredPermission的注解
                RequiredPermission anno = method.getAnnotation(RequiredPermission.class);
                //判断是否有注解,有的话,获取该注解的value值,然后把数据分装到到Permission对象中
                if(anno != null && !exps.contains(expression)){//判断是否有贴注解 , 并且判断该权限表达式是否存在
                    //创建一个Permission对象
                    Permission permission = new Permission();
                    permission.setName(anno.value());//设置名称为注解的value值
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);
                }

            }
        }
    }
}
